package map;

import java.util.*;

/**
 * Created with IntelliJ IDEA
 * Description:Map接口学习(结合HashMap)
 * User:22954
 * Date:2024-02-12
 * Time:2:55
 */
@SuppressWarnings("all")
public class Map_ {
    /*
    1.Map是一个接口,有实现类HashMap  **Map和Collection是两个不同接口,本身并没有相关
    2.Map中的<K,V>可以是任意引用类型对象,被分装到HashMap$Node对象中
    3.Map中的key值不能重复,value值可以重复
    4.put()输入已存在的key值时,会将新value值替换掉旧value值 (e=p)
    5.Map中key值和value值都可以为null,但是只能存入一个key值为null
    6.常用String类作为Map的<K, >
    ============================
    在Map中创建了Entry<K,V>子接口,在HashMap中Node<K,V>实现该接口,用于存放键值对

    为了方便遍历,Map接口定义了keySet()和values()方法,底层为返回一个Set指向key,一个Collection指向value
    为了方便遍历,HashMap定义了entrySet()方法,返回Set<Entry<K,V>>,实际是一个多态,返回Set<Node<K,V>>--
    --Entry<K,V>接口下定义了getKey(),getValue()方法,均在HashMap中的Node<K,V>下实现--
    --当转型为Set<Node<K,V>>时,实现了Map接口和Collection接口的联系,使得Map接口下的HashMap可以用迭代器遍历--
    --同时因为Set中存放的都是Node<K,V>,可以用getKey(),getValue()方法获取<K,V>,形成回环,便于遍历HashMap
     */
    public static void main(String[] args) {
        /*
        遍历HashMap方法
        1.Set指向key,增强for和迭代器遍历
        2.Collection指向value,增强for和迭代器遍历(不能直接获取key值)
        3.Set<Entry<K,V>>指向Node<K,V>,增强for和迭代器遍历
        一共6种简单遍历方式
         */
        HashMap<String, Integer> map = new HashMap<>();
        map.put("zhangsan", 1);
        map.put("wanger", 2);
        map.put("lisi", 3);
        map.put("wangwu", 4);
        //1.取出所有key值
        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.println(key + "=" + map.get(key));
        }
        System.out.println("============");
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println(key + "=" + map.get(key));
        }
        System.out.println("============");
        //2.取出所有value值
        Collection<Integer> values = map.values();
        /*
        不支持直接从value值查找key值
         */
        for (Integer value : values) {
            System.out.println(value);
        }
        System.out.println("============");
        Iterator<Integer> iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object value = iterator1.next();
            System.out.println(value);
        }
        //3.通过EntrySet
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        System.out.println("============");
        Iterator<Map.Entry<String, Integer>> iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Map.Entry<String, Integer> entry = iterator2.next();
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }

    public static void main4(String[] args) {
        /*
        Map常用方法,put(),remove(),get(),size(),isEmpty(),containsKey(),clear()
         */
        HashMap<String, Integer> map = new HashMap<>();
        map.put("zhangsan", 1);
        map.put("wanger", 2);
        map.put("lisi", 3);
        map.put("wangwu", 4);
        System.out.println("map=" + map);//map={lisi=3, zhangsan=1, wangwu=4, wanger=2}
        map.remove("wanger");
        System.out.println("map=" + map);//map={lisi=3, zhangsan=1, wangwu=4}
        Integer val = map.get("lisi");
        System.out.println("value of lisi=" + val);//value of lisi=3
        System.out.println("map size=" + map.size());//map size=3
        System.out.println("map is it empty:" + map.isEmpty());//map is it empty:false
        map.containsKey("is map cotain key\"wangwu\":" + map.containsKey("wangwu"));//{}
        map.clear();
        System.out.println(map);

    }

    public static void main3(String[] args) {
        HashMap<Integer, Car> intCarHashMap = new HashMap<>();
        intCarHashMap.put(1, new Car(1));
        intCarHashMap.put(2, new Car(2));
        intCarHashMap.put(3, new Car(3));
        Set<Integer> integers = intCarHashMap.keySet();
        for (Integer integer : integers) {
            System.out.println("key=" + integer);
            //key=1
            //key=2
            //key=3
        }
        Collection<Car> values = intCarHashMap.values();
        for (Car value : values) {
            System.out.println("value=" + value);
            //value=map.Car@7291c18f
            //value=map.Car@34a245ab
            //value=map.Car@7cc355be
        }
    }

    public static void main2(String[] args) {
        HashMap<Integer, Car> carHashMap = new HashMap<>();
        carHashMap.put(1, new Car(1));
        carHashMap.put(2, new Car(2));
        carHashMap.put(3, new Car(3));
        Set<Map.Entry<Integer, Car>> entries = carHashMap.entrySet();
        /*
        1.Map.Entry<K,V>是Map下定义的子接口，在HashMap中重载为Node<K,V>
        2.实现该接口方便于遍历HashMap
         */
        for (Map.Entry<Integer, Car> entry : entries) {
            System.out.println(entry.toString());
            //1=map.Car@2f4d3709
            //2=map.Car@4e50df2e
            //3=map.Car@1d81eb93
        }
    }

    public static void main1(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "aa");
        map.put(2, "bb");
        map.put(3, "aa");
        map.put(1, "dd");
        map.put(null, null);
        map.put(4, null);
        System.out.println(map);
    }
}

class Car {
    int price;

    public Car(int price) {
        this.price = price;
    }
}